package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author：cxl
 * @Package：com.itheima.service.impl
 * @Project：web-project
 * @name：ClazzServiceImpl
 * @Date：2025-09-25 5:48 pm
 * @Filename：ClazzServiceImpl
 */

@Service
public class ClazzServiceImpl implements ClazzService {
    private static final Logger log = LoggerFactory.getLogger(ClazzServiceImpl.class);
    @Autowired
    ClazzMapper clazzMapper;

    @Override
    public List<Clazz> selectAll() {
        List<Clazz> clazzList = clazzMapper.selectAll();
        return clazzList;
    }

    @Override
    public void add(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(clazz.getCreateTime());
        clazzMapper.add(clazz);
        System.out.println(clazz);
    }

    @Override
    public PageResult<Clazz> page(ClazzQueryParam clazzQueryParam) {
        //首先设置PageHelper的初始参数
        PageHelper.startPage(clazzQueryParam.getPage(), clazzQueryParam.getPageSize());
        //这一行的返回结果会被分页，返回结果就是一个Page对象
        Page<Clazz> pageClazz = clazzMapper.page(clazzQueryParam);
      /*  List<Clazz> clazzList = pageClazz.getResult();
        LocalDate localDate = LocalDate.now();
        for (Clazz clazz : clazzList) {
            if (clazz.getBeginDate() != null && clazz.getEndDate() != null) {
                if (localDate.isBefore(clazz.getBeginDate())) {
                    clazz.setStatus("未开班");
                } else if (localDate.isBefore(clazz.getEndDate())) {
                    clazz.setStatus("已开班");
                } else {
                    clazz.setStatus("已结课");
                }
            }else {
                clazz.setStatus("未知状态");
            }
            log.info("状态{}", clazz.getStatus());
        }*/
        return new PageResult<>(pageClazz.getTotal(), pageClazz.getResult());
    }

    @Override
    public void deleteById(Integer id) {
        clazzMapper.deleteById(id);
    }

    @Override
    public Clazz selectById(Integer id) {
      Clazz clazz=  clazzMapper.selectById(id);
        return clazz;
    }

    @Override
    public Clazz update(Clazz clazz) {
        clazzMapper.update(clazz);
        return clazz;
    }
}
